package net.llz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import net.llz.enums.AddressStatusEnum;
import net.llz.interceptor.LoginInterceptor;
import net.llz.mapper.AddressMapper;
import net.llz.model.AddressDO;
import net.llz.model.LoginUser;
import net.llz.request.AddressAddReqeust;
import net.llz.service.AddressService;
import net.llz.vo.AddressVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressMapper addressMapper;

    @Override
    public AddressVO detail(Long id) {

        LoginUser loginUser = LoginInterceptor.threadLocal.get();

       AddressDO addressDO=  addressMapper.selectOne(new QueryWrapper<AddressDO>().eq("id" ,id).eq("user_id",loginUser.getId()));

        if (addressDO == null) {
            return null;
        }
        AddressVO addressVO = new AddressVO();
        BeanUtils.copyProperties(addressDO,addressVO);

      return addressVO;
    }

    /**
     * 新增收获地址
     * @param addressAddReqeust
     */
    @Override
    public void add(AddressAddReqeust addressAddReqeust) {
      LoginUser loginUser =  LoginInterceptor.threadLocal.get();

      AddressDO addressDO = new AddressDO();
      addressDO.setCreateTime(new Date());
      addressDO.setUserId(loginUser.getId());

        BeanUtils.copyProperties(addressAddReqeust,addressDO);

        //是否有默认地址
        if(addressDO.getDefaultStatus() == AddressStatusEnum.DEFAULT_STATUS.getStatus()){
            //查找数据库默认地址
            AddressDO defaultAddressDO  = addressMapper.selectOne(new QueryWrapper<AddressDO>()
                    .eq("user_id",loginUser.getId())
                    .eq("default_status",AddressStatusEnum.DEFAULT_STATUS.getStatus()));
            if (defaultAddressDO != null){
                //修改成非收获地址
                defaultAddressDO.setDefaultStatus(AddressStatusEnum.COMMON_STATUS.getStatus());
                addressMapper.update(defaultAddressDO,new QueryWrapper<AddressDO>().eq("id",defaultAddressDO.getId()));
            }

        }

        int rows = addressMapper.insert(addressDO);

        log.info("新增收获地址{},data{}",rows,addressDO);

    }

    @Override
    public Integer del(Integer address_id) {

        LoginUser loginUser = LoginInterceptor.threadLocal.get();

        Integer rows = addressMapper.delete(new QueryWrapper<AddressDO>().eq("id",address_id).eq("user_id",loginUser.getId()));

        return rows;
    }

    @Override
    public List<AddressVO> listUserAllAddress() {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();

        List<AddressDO> list = addressMapper.selectList(new QueryWrapper<AddressDO>().eq("user_id",loginUser.getId()));

        List<AddressVO> addressVOList = list.stream().map(obj->{
            AddressVO addressVO = new AddressVO();
            BeanUtils.copyProperties(obj,addressVO);
            return addressVO;
        }).collect(Collectors.toList());
        return addressVOList;
    }
}
